package com.ruoyi.worker.service.workerSys.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.worker.domain.TimeShift;
import com.ruoyi.worker.domain.workAttendance.AttendanceSalary;
import com.ruoyi.worker.domain.workAttendance.RzAttendanceSalarySum;
import com.ruoyi.worker.mapper.TimeShiftMapper;
import com.ruoyi.worker.mapper.attendance.AttendanceSalaryMapper;
import com.ruoyi.worker.mapper.attendance.RzAttendanceSalarySumMapper;
import com.ruoyi.worker.service.workerSys.AttendanceSalaryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AttendanceSalaryServiceImpl
 * @Description 长期工工资计算
 * @Author leizhang
 * Date 2021/11/28 9:06 上午
 * @Version 1.0
 **/
@Service
@Slf4j
public class AttendanceSalaryServiceImpl implements AttendanceSalaryService {

    @Resource
    private AttendanceSalaryMapper attendanceSalaryMapper;

    @Resource
    private RzAttendanceSalarySumMapper rzAttendanceSalarySumMapper;

    @Resource
    private TimeShiftMapper timeShiftMapper;

    /**
     * @param attendanceSalary
     * @return void
     * @Description : 长期工工资计算
     * @Author : leizhang
     * @Date 9:06 上午 2021/11/28
     **/
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int attendanceSalaryCalculate(AttendanceSalary attendanceSalary) {
        //1.岗位信息查询
        String workTime = attendanceSalary.getWorkTime().concat("-01");
        attendanceSalary.setWorkTime(workTime);
        SysDept requestParam = new SysDept();
        requestParam.setCategory(5);
        List<SysDept> deptRecords = attendanceSalaryMapper.getDeptRecords(requestParam);
        //2.班次单价获取
        List<TimeShift> timeShiftList = timeShiftMapper.getTimeShiftList(new TimeShift());
        //3.考勤信息查询
        List<AttendanceSalary> attendanceSalaries = attendanceSalaryMapper.listOfAttendanceSalary(attendanceSalary);
        if (attendanceSalaries.isEmpty()) {
            return -1;
        }
        attendanceSalaries.stream().forEach(salary -> {
            salary.setDeptName(attendanceSalary.getDeptName());
            salary.setDeptId(attendanceSalary.getDeptId());
            salary.setSysDept(deptRecords);
            salary.setTimeShiftList(timeShiftList);
            salary.setWorkTime(workTime);
            salary.setCreateBy(SecurityUtils.getUsername());
            try {
                salary.calculateSalary();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        //4.保存汇总信息
        RzAttendanceSalarySum rzAttendanceSalarySum = new RzAttendanceSalarySum(Integer.parseInt(attendanceSalary.getDeptId()), attendanceSalary.getDeptName(), attendanceSalaries, workTime);
        rzAttendanceSalarySumMapper.insertRzAttendanceSalarySum(rzAttendanceSalarySum);
        //5.工资保存
        attendanceSalaryMapper.saveAttendanceSalary(attendanceSalaries, rzAttendanceSalarySum.getId());
        return 1;
    }

    /**
     * 查询长期工工资
     *
     * @param id 长期工工资主键
     * @return 长期工工资
     */
    @Override
    public AttendanceSalary selectRzAttendanceSalaryById(int id) {
        return attendanceSalaryMapper.selectRzAttendanceSalaryById(id);
    }

    /**
     * 查询长期工工资列表
     *
     * @param rzAttendanceSalary 长期工工资
     * @return 长期工工资
     */
    @Override
    public List<AttendanceSalary> selectRzAttendanceSalaryList(AttendanceSalary rzAttendanceSalary) {
        return attendanceSalaryMapper.selectRzAttendanceSalaryList(rzAttendanceSalary);
    }

    /**
     * 修改长期工工资
     *
     * @param rzAttendanceSalary 长期工工资
     * @return 结果
     */
    @Override
    public int updateRzAttendanceSalary(AttendanceSalary rzAttendanceSalary) {
        //长期工个人信息修改
        //1.前提只修改  补发工资 月度绩效 月度考核奖励  考勤扣款 工装扣款 月度考核扣款  备注
        //2. 三个加法：补发工资（back_pay ） 月度绩效（monthly_performance） 月度考核奖励（monthly_assessment_rewards）
        // 三个减法:月度考核扣款（assessment_deduction） 工装扣款（tooling_deduction） 考勤扣款（attendance_deduction）
        //应发工资 = 补发工资（back_pay ） + 月度绩效（monthly_performance） +  月度考核奖励（monthly_assessment_rewards）
        //实发工资 = 月度考核扣款（assessment_deduction）+ 工装扣款（tooling_deduction）+ 考勤扣款（attendance_deduction）
        AttendanceSalary attendanceSalary = attendanceSalaryMapper.selectRzAttendanceSalaryById(rzAttendanceSalary.getId());
        BigDecimal backPay = rzAttendanceSalary.getBackPay();
        BigDecimal monthlyPerformance = rzAttendanceSalary.getMonthlyPerformance();
        BigDecimal monthlyAssessmentRewards = rzAttendanceSalary.getMonthlyAssessmentRewards();
       BigDecimal newAddMoney = backPay.add(monthlyPerformance).add(monthlyAssessmentRewards);

        BigDecimal assessmentDeduction = rzAttendanceSalary.getAssessmentDeduction();
        BigDecimal toolingDeduction = rzAttendanceSalary.getToolingDeduction();
        BigDecimal attendanceDeduction = rzAttendanceSalary.getAttendanceDeduction();
        BigDecimal newSubMoney = assessmentDeduction.add(toolingDeduction).add(attendanceDeduction);

        BigDecimal oldAddMoney = attendanceSalary.getBackPay().add(attendanceSalary.getMonthlyPerformance()).add(attendanceSalary.getMonthlyAssessmentRewards());
        BigDecimal oldSubMoney = attendanceSalary.getAssessmentDeduction().add(attendanceSalary.getToolingDeduction()).add(attendanceSalary.getAttendanceDeduction());
        //应发工资
        BigDecimal payable = attendanceSalary.getPayable();
        attendanceSalary.setPayable(payable.add(newAddMoney).subtract(oldAddMoney));
        //实发工资
        attendanceSalary.setPaidWages(attendanceSalary.getPayable().subtract(newSubMoney).add(oldSubMoney));
        attendanceSalary.setBackPay(backPay);
        attendanceSalary.setMonthlyPerformance(monthlyPerformance);
        attendanceSalary.setMonthlyAssessmentRewards(monthlyAssessmentRewards);
        attendanceSalary.setAssessmentDeduction(assessmentDeduction);
        attendanceSalary.setToolingDeduction(toolingDeduction);
        attendanceSalary.setAttendanceDeduction(attendanceDeduction);
        return attendanceSalaryMapper.updateRzAttendanceSalary(attendanceSalary);
    }
}
